Ext.ux.AddCategory = Ext.extend(Ext.Panel, {
	initComponent : function(config) {
		var basePath = document.getElementById('basePath').href;
		var lang = '';
		var parent = '';
		var who = '';
		var exist = '';
		
		var langStore = new Ext.data.ArrayStore({
	        fields: ['lang', 'value'],
	        data : [['简体中文', 'zh_CN'],['繁体中文', 'zh_TW'],['美式英语', 'en_US']]
	    });
		
		var whoCategoryStore = new Ext.data.ArrayStore({
	        fields: ['who', 'value'],
	        data : [['产品类别', 'product'],['设备类别', 'equipment'],['新闻类别', 'news']]
	    });
		
		var category = new Ext.data.Store( {
			proxy: new Ext.data.HttpProxy({url: basePath + 'base/listParent.action'}),
			reader:new Ext.data.ArrayReader({idIndex: 0},
				Ext.data.Record.create([{name: 'id'}, {name: 'name'}]))
		});
		
		var addCategoryForm = new Ext.FormPanel({
			labelWidth : 100,
			url : basePath + 'category/addCategory.action',
			autoScroll:true,
			frame : true,
			fileUpload: true,
			bodyStyle : 'padding:5px 5px 0',
			width : '100%',
			defaults : {
				width : 220
			},
			defaultType : 'textfield',

			items : [ {
				xtype : "combo",
				fieldLabel : '选择语言',
				store : langStore,
				mode : 'local',
				valueField: 'value',
				displayField : 'lang',
				allowBlank : false,
				typeAhead : true,
				triggerAction : 'all',
				editable:false,
				forceSelection : true,
				selectOnFocus : true,
				listeners:{
					select : function(combo, record, index) {
						lang = record.get('value');
					}
				}
			}, {
				fieldLabel : '类别名称',
				name : 'category.name',
				allowBlank : false,
				listeners:{
					change :function(field) {
						Ext.Ajax.request( {
							url : encodeURI(encodeURI(basePath + 'base/checkExist.action?categoryName='
									+ field.getValue() + '&lang=' + lang)),
							method : 'post',
							callback : function(op, success, response) {
								var msg = eval('(' + response.responseText + ')');
								exist = msg.result;
								if (msg.result == 'true') {
									Ext.Msg.alert('提示', '该类别名已存在');
									return;
								}
							}
						});
					}
				}
			}, {
				xtype : "combo",
				fieldLabel : '选择类别归属',
				store : whoCategoryStore,
				mode : 'local',
				valueField: 'value',
				displayField : 'who',
				allowBlank : false,
				typeAhead : true,
				triggerAction : 'all',
				editable:false,
				forceSelection : true,
				selectOnFocus : true,
				listeners:{
					select : function(combo, record, index) {
						category.removeAll();
						who = record.get('value');
						category.proxy.setUrl(basePath + 'base/listParent.action?whoCategory='
							+ who + '&lang=' + lang);
						category.load();
					}
				}
			}, {
				xtype : "combo",
				fieldLabel : '选择父类别',
				store : category,
				mode : 'local',
				valueField: 'value',
				displayField : 'name',
				typeAhead : true,
				id:'parentCategory',
				triggerAction : 'all',
				editable:false,
				forceSelection : true,
				selectOnFocus : true,
				listeners:{
					select : function(combo, record, index) {
						Ext.getCmp('parentCategory').setValue(record.get('name'));
						parent = record.get('id');
					}
				}
			}, {
				xtype:'textarea',
				fieldLabel : '类别简介',
				anchor:'95%',
				height:250,
				allowBlank : false,
				autoScroll:true,
				name : 'category.notes'
			}],
			bbar : ['->', {
				text : '保存',
				handler:function() {
					if(!addCategoryForm.form.isValid()) return;
					if(exist == 'true') {
						Ext.Msg.alert('提示', '该类别名已存在');
						return;
					}
					addCategoryForm.form.submit( {
						url : basePath + 'base/addCategory.action?category.lang=' + lang + 
								'&category.whoCategory=' + who + '&parentId=' + parent,
						success : function(form, action) {
							form.reset();
							lang = '';
							parent = '';
							who = '';
							exist = '';
							Ext.Msg.alert('提示', '添加类别成功');
						},
						failure : function(form, action) {
							form.reset();
							if (action.failureType == Ext.form.Action.SERVER_INVALID)
								Ext.MessageBox.alert('警告', action.result.msg);
						}
					});
				}
			}, '-', {
				text : '重置',
				handler:function() {
					addCategoryForm.form.reset();
					lang = '';
					parent = '';
					who = '';
					exist = '';
				}
			}, {
				xtype:'spacer',
				width:50
			}]
		});
		
		Ext.apply(this, {
			title : '添加类别',
			autoScroll:true,
			closable : 'true',
			frame : true,
			bodyStyle : 'padding:5px 5px 0',
			width : 370,
			items:[addCategoryForm]
		});
		Ext.ux.AddCategory.superclass.initComponent.apply(this, arguments);
	}
});
Ext.reg('lgnAddCategory', Ext.ux.AddCategory);
